/*
 * adc_dirver.c
 *
 *  Created on: Oct 28, 2021
 *      Author: 王慧云
 *      依赖 adc.c
 */

#include "adc_dirver.h"
//获得ADC值
//ch: 通道值 0~16，取值范围为：ADC_CHANNEL_0~ADC_CHANNEL_16
//返回值:转换结果
uint16_t get_adc(ADC_HandleTypeDef adc,uint32_t ch)
{
	ADC_ChannelConfTypeDef ADC_ChanConf;
	ADC_ChanConf.Channel =ch;
//	ADC_ChanConf.Offset = 0;
//	ADC_ChanConf.SamplingTime = DC_SAMPLETIME_480CYCLES;

	HAL_ADC_ConfigChannel(&adc, &ADC_ChanConf);

	HAL_ADC_Start(&adc);

	HAL_ADC_PollForConversion(&adc, 10);

	return (uint16_t)HAL_ADC_GetValue(&adc);
}

//获取指定通道的转换值，取times次,然后平均
//times:获取次数
//返回值:通道ch的times次转换结果平均值====++

uint16_t get_adc_average(ADC_HandleTypeDef hadc,uint32_t ch,uint8_t times)
{
    uint32_t temp_val=0;
    uint8_t t;
    for(t=0;t<times;t++)
    {
        temp_val+=get_adc(hadc,ch);
        HAL_Delay(5);
    }
    return temp_val/times;
}

